﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Common;

namespace childcare
{
    public partial class frmquanlytaikhoan : Form
    {

        SqlConnection con = new SqlConnection();
        private DataTable dt = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();

        public frmquanlytaikhoan()
        {
            InitializeComponent();
        }

        private void frmquanlytaikhoan_Load(object sender, EventArgs e)
        {
            string connect = @"Data Source=BLACK-PC\SQLEXPRESS;Initial Catalog=Childcare;Integrated Security=True";
            con = new SqlConnection(connect);
            connectdatabase.Openconnect();
            dgvdanhsachtk.DataSource = connectdatabase.ex("select * from login");
            databinding();
        }


        private void databinding()//bing dinh du lieu
        {
            txtuser.DataBindings.Clear();
            txtuser.DataBindings.Add("Text", dgvdanhsachtk.DataSource, "user");
            txtpassword.DataBindings.Clear();
            txtpassword.DataBindings.Add("Text", dgvdanhsachtk.DataSource, "password");
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
            frmadmin _frmadmin = new frmadmin();
            _frmadmin.Show();
        }

      
        private void btnthemtk_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand commandinsert = new SqlCommand();
                commandinsert.Connection = con;
                commandinsert.CommandType = CommandType.Text;
                commandinsert.CommandText = "insert [login] ([user],[password] ) values(@user,@password)";
                SqlParameter pram = commandinsert.Parameters.Add("@user", SqlDbType.VarChar, 30);
                pram.Value = txtuser.Text;
                pram = commandinsert.Parameters.Add("@password", SqlDbType.VarChar, 20);
                pram.Value = txtpassword.Text;
                con.Open();
                commandinsert.ExecuteNonQuery();
              //  con.Close();
                dgvdanhsachtk.DataSource = connectdatabase.ex("select * from login");
              //  databinding();
                MessageBox.Show("Bạn đã thêm thành công!", "Thông Báo", MessageBoxButtons.OK);
            }
            catch
            {
                MessageBox.Show("Tài khoản này đã tồn tại rồi!", "Thông Báo", MessageBoxButtons.OK);
            }
            finally
            {
                con.Close();
                databinding();
            }
        }

        private void btnsuatk_Click(object sender, EventArgs e)
        {
            SqlCommand commandupdate = new SqlCommand();
            commandupdate.Connection = con;
            commandupdate.CommandType = CommandType.Text;
            commandupdate.CommandText = @"update [login] set [password] = @password where [user] = @user";
            SqlParameter pram = commandupdate.Parameters.Add("@user", SqlDbType.VarChar, 30);
            pram.Value = txtuser.Text;
            pram = commandupdate.Parameters.Add("@password", SqlDbType.VarChar, 20);
            pram.Value = txtpassword.Text;
            con.Open();
            commandupdate.ExecuteNonQuery();
            con.Close();
            dgvdanhsachtk.DataSource = connectdatabase.ex("select * from login");
            MessageBox.Show("Bạn đã sửa thành công!", "Thông Báo", MessageBoxButtons.OK);
            databinding();
        }

        private void btnxoatk_Click(object sender, EventArgs e)
        {
            SqlCommand commandinsert = new SqlCommand();
            commandinsert.Connection = con;
            commandinsert.CommandType = CommandType.Text;
            commandinsert.CommandText = @"delete from [login] where [user] = @user";
            SqlParameter pram = commandinsert.Parameters.Add("@user", SqlDbType.VarChar, 30);
            pram.Value = txtuser.Text;
            con.Open();
            commandinsert.ExecuteNonQuery();
            con.Close();
            dgvdanhsachtk.DataSource = connectdatabase.ex("select * from [login]");
            MessageBox.Show("Bạn đã Xóa thành công!", "Thông Báo", MessageBoxButtons.OK);
            databinding();
        }

        
    }
}